package com.pams.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.pams.context.BaseContext;
import com.pams.handler.BaseException;
import com.pams.mapper.TaskMapper;
import com.pams.pojo.entity.Tasks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class TaskService {
    @Autowired
    private TaskMapper taskMapper;
    public void add(Tasks tasks) {
        String currentUsername = BaseContext.getCurrentUsername();
        tasks.setUsername(currentUsername);
        if(tasks.getTitle() == null || tasks.getTitle().equals("" )) throw new BaseException("请填写标题");
        taskMapper.save(tasks);
    }

    public List<Tasks> list() {
        String currentUsername = BaseContext.getCurrentUsername();
        List<Tasks> list =  taskMapper.list(currentUsername);

        return list;
    }

    public void delete(Long id) {
        taskMapper.delete(id);
    }

    public void update(Tasks tasks) {
        taskMapper.update(tasks);
    }

    public void updateStatus(Long id, Integer status) {
        if(status != 1 ){
            status = 1;
        }else {
            status = -1;
        }
        taskMapper.updateStatus(id, status);
    }

    public Tasks getTaskById(Long id) {
        return taskMapper.getTaskById(id);
    }

    public List<Tasks> getTodayTask() {
        LocalDate now = LocalDate.now();
        String currentUsername = BaseContext.getCurrentUsername();
        return taskMapper.getTodayTask(currentUsername);
    }

    public String getStatusToday(LocalDateTime day){

        String currentUsername = BaseContext.getCurrentUsername();
        List<Tasks> todayTask = taskMapper.getdayTask(currentUsername ,day);

        boolean hasCompleted = false;
        boolean hasUncompleted = false;
        for (Tasks task : todayTask) {
            if(task.getComleted() == 1){
        hasCompleted = true;
    }else {
        hasUncompleted = true;
    }
}

        if (hasCompleted && !hasUncompleted) {
            return "completed"; // 全部完成
        } else if (!hasCompleted && hasUncompleted) {
            return "uncompleted"; // 全部未完成
        } else {
            return "completing"; // 部分完成
        }
    }
}
